package com.panghu.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.panghu.entity.ApiResult;
import com.panghu.entity.Message;
import com.panghu.entity.Replay;
import com.panghu.mapper.ReplayMapper;
import com.panghu.service.MessageService;
import com.panghu.service.ReplayService;
import com.panghu.service.impl.MessageServiceImpl;
import com.panghu.utils.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 留言表 前端控制器
 * </p>
 *
 * 查询全部评论内容
 *1. 在主评论表中，进行所有主评论的查询
 * 2.在在附加评论表中，查询所有的内容
 *
 * @author huahua
 * @since 2022-03-19
 */
@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Resource
    private ReplayService replayService;

    @GetMapping("/messages/{page}/{size}")
    public ApiResult<Message> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        //查询所有的评论 包括追加评论 需要进行多表查询
         Page<Message> p = new Page<>(page,size);
         IPage iPage = messageService.findAll(p);
        return ApiResultHandler.success(iPage);
    }

    @GetMapping("/message/{id}")
    public ApiResult findById(@PathVariable("id") Integer id) {
        //按id进行评论查询
        Message message = messageService.findById(id);
        return ApiResultHandler.success(message);
    }

    @DeleteMapping("/message/{id}")
    public ApiResult delete(@PathVariable("id") Integer id) {
        //按id进行评论删除
        boolean b = messageService.removeById(id);
        Map<String,Object> map = new HashMap<String,Object>();
        //按照message的id进行删除
        map.put("messageId",id);
        boolean i = replayService.removeByMap(map);
        return ApiResultHandler.success();
    }

    @PostMapping("/message")
    public ApiResult add(@RequestBody Message message) {
        //接受前端发送的数据 进行评论添加
        boolean save = messageService.save(message);
        return ApiResultHandler.success(save);
    }


}

